//
//  DetailViewController.m
//  bank_ipad2
//
//  Created by nxj bill on 12-3-9.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "DetailViewController.h"
#import "RootViewController.h"
#import "Constants.h"
#import "BankMainMenu.h"

//static NSString *buttonTitle = @"Top Paidddddddd Apps";



@interface DetailViewController ()
@property (nonatomic, retain) UIPopoverController *popoverController;
- (void)configureView;
@end



@implementation DetailViewController

@synthesize toolbar, popoverController, detailItem, detailDescriptionLabel,detailDescriptionWeb,otherWeb,root;

#pragma mark -
#pragma mark Managing the detail item



// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/* 
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization.
 }
	 int i;
	 i=1;
 return self;
 }
*/

//- (void)awakeFromNib
//{
//    self.splitViewController.delegate = self;
////    self.masterViewController.contentController = self;
//    
//    // configure the master popover button:
//    //
//    // In portrait mode (when the master view is not being displayed by the split view controller)
//    // add a bar button item to display the master view in a popover; in landcape mode, remove the button.
//    //
//    if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
//    {
//        UIBarButtonItem *popoverMasterViewControllerBarButtonItem =
//        [[UIBarButtonItem alloc] initWithTitle:buttonTitle
//                                         style:UIBarButtonItemStylePlain
//                                        target:self
//                                        action:@selector(presentMasterInPopoverFromBarButtonItem:)];
////        [self.detailViewController.navBar.topItem setLeftBarButtonItem:popoverMasterViewControllerBarButtonItem animated:YES];
////		[self
//        [popoverMasterViewControllerBarButtonItem release];
//    }
//    else
//    {
////        [self.detailViewController.navBar.topItem setLeftBarButtonItem:nil animated:YES];
//    }
//}



- (void)action:(id)sender
{
	int i;
	i=1;

	
	
	
	//TabDefaultController *uiCtrl = [super createTabController:kTabBankLoginControllerTag];
	//ui需要nav控制
	//UIViewController *uiCtrl = [[Login alloc] initWithNibName:@"LoginNew" bundle:nil];
//	UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:self] autorelease];
//	[[self navigationController] pushViewController:q animated:YES];
	//[uiCtrl release];
	
	//return nav;
	
	
	
}

/*
 When setting the detail item, update the view and dismiss the popover controller if it's showing.
 */
- (void)setDetailItem:(id)newDetailItem {
    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];
        
        // Update the view.
        [self configureView];
    }

    if (self.popoverController != nil) {
        [self.popoverController dismissPopoverAnimated:YES];
    }        
}


- (void)configureView {
    // Update the user interface for the detail item.
    detailDescriptionLabel.text = [detailItem description]; 
	


}


#pragma mark -
#pragma mark Split view support

- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {
    barButtonItem.title = @"河北银行";
    NSMutableArray *items = [[toolbar items] mutableCopy];
    [items insertObject:barButtonItem atIndex:0];
    [toolbar setItems:items animated:YES];
    [items release];
    self.popoverController = pc;
}


// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
    
    NSMutableArray *items = [[toolbar items] mutableCopy];
    [items removeObjectAtIndex:0];
    [toolbar setItems:items animated:YES];
    [items release];

    self.popoverController = nil;
}


#pragma mark -
#pragma mark Rotation support

// Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
	
    interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}


#pragma mark -
#pragma mark View lifecycle


 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSString *filePathOfIndex = [[NSBundle mainBundle] pathForResource:@"otherWeb.html" ofType:nil];
	NSString *strAbsPath= [NSString stringWithFormat:@"encodeURI('file:///%@')",filePathOfIndex];
	NSString *url  = [otherWeb stringByEvaluatingJavaScriptFromString:strAbsPath];
	[otherWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
	

	
//	BankMainMenu *object = [[BankMainMenu alloc] initWithNibName:@"BankMainMenu" bundle:nil];
//	//[[self navigationController] pushViewController:object animated:YES];
//	UIPopoverController *popoverController1;
//	popoverController1 = [[UIPopoverController alloc] initWithContentViewController:object];
//	
//	
//	
//	[object release];
	
	
//	UIViewController *p = [(bank_ipad2AppDelegate*)[[UIApplication sharedApplication] delegate] rootViewController];
	
//	self.title=@"adfasdf";
//	self.navigationController.title=@"adfasdf";
//	self.tabBarController.title=@"tabbarcontrollert";
//	self.parentViewController.title=@"tabbarcontrollert";
	
	
//	CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
//	webFrame.origin.y += 25.0;	// leave from the URL input field and its label
//	webFrame.size.width -= 70.0;	// leave from the URL input field and its label
//	webFrame.size.height -= 10.0;
//	
//	detailDescriptionWeb.frame = webFrame;
	//self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
//	self.detailDescriptionWeb.backgroundColor = [UIColor whiteColor];
//	self.detailDescriptionWeb.scalesPageToFit = YES;
//	self.detailDescriptionWeb.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
//	self.detailDescriptionWeb.delegate = self;
	//[self.view addSubview: self.myWebView];
	
}

//#pragma mark -
//#pragma mark UIViewController delegate methods

- (void)viewWillAppear:(BOOL)animated
{
	self.detailDescriptionWeb.delegate = self;	// setup the delegate as the web view is shown
	self.otherWeb.delegate = self;	// setup the delegate as the web view is shown

}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.detailDescriptionWeb stopLoading];	// in case the web view is still loading its content
	self.detailDescriptionWeb.delegate = nil;	// disconnect the delegate as the webview is hidden
	[self.otherWeb stopLoading];
	self.otherWeb.delegate = nil;
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}



/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.popoverController = nil;
}


#pragma mark -
#pragma mark Memory management

/*
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
*/


- (void)dealloc {
    [popoverController release];
    [toolbar release];
    
    [detailItem release];
    [detailDescriptionLabel release];
	[detailDescriptionWeb release];
	[otherWeb release];
    [super dealloc];
}



#pragma mark -
#pragma mark UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
	// starting the load, show the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	
	
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	// finished loading, hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
		NSString *variableOfWap = [webView stringByEvaluatingJavaScriptFromString:@"login"];

	if(![variableOfWap compare:@"ok"]){
			NSLog(variableOfWap);
		    [root pushMenu];

	}
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
	// load error, hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	
	// report the error inside the webview
	NSString* errorString = [NSString stringWithFormat:
							 @"<html><center><font size=+5 color='red'>An error occurred:<br>%@</font></center></html>",
							 error.localizedDescription];
	//[self.detailDescriptionWeb loadHTMLString:errorString baseURL:nil];
}

@end
